package cn.zl.GB28181.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.zl.GB28181.utils.DateUtils;
import cn.zl.model.DeviceChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DeviceChannelDataHandle {

    private static final Logger logger = LoggerFactory.getLogger(DeviceChannelDataHandle.class);

    // 当前设备下所有的通道
    private final Map<String, DeviceChannel> allChannelMap = new HashMap<>();
    // 数据去重后列表
    private final List<DeviceChannel> channels = new ArrayList<>();
    // 重复数据
    private final StringBuilder duplicateData = new StringBuilder();

    // 需要新增列表
    private final List<DeviceChannel> addChannels = new ArrayList<>();
    // 需要更新列表
    private final List<DeviceChannel> updateChannels = new ArrayList<>();
    // 需要删除列表
    private final List<DeviceChannel> deleteChannels = new ArrayList<>();

    public void setAllChannelMap(List<DeviceChannel> allChannels) {
        if (CollectionUtil.isNotEmpty(allChannels)) {
            for (DeviceChannel deviceChannel : allChannels) {
                allChannelMap.put(deviceChannel.getChannelId(), deviceChannel);
            }
        }
    }

    public void setDeviceChannelList(List<DeviceChannel> deviceChannelList) {
        if (CollectionUtil.isEmpty(deviceChannelList)) {
            return;
        }
        Set<String> gbIdSet = new HashSet<>();
        for (DeviceChannel deviceChannel : deviceChannelList) {
            if (gbIdSet.contains(deviceChannel.getChannelId())) {
                duplicateData.append(deviceChannel.getChannelId()).append(",");
                continue;
            }
            gbIdSet.add(deviceChannel.getChannelId());
            // 查询是否在数据库  存在 则添加到 updateChannels 列表
            // 不存在 addChannels 列表
            if (allChannelMap.containsKey(deviceChannel.getChannelId())) {
                deviceChannel.setHasAudio(allChannelMap.get(deviceChannel.getChannelId()).isHasAudio());
                deviceChannel.setUpdateTime(DateUtils.getNow());
                updateChannels.add(deviceChannel);
            } else {
                deviceChannel.setCreateTime(DateUtils.getNow());
                deviceChannel.setUpdateTime(DateUtils.getNow());
                addChannels.add(deviceChannel);
            }
            allChannelMap.remove(deviceChannel.getChannelId());
            channels.add(deviceChannel);
        }
        deleteChannels.addAll(allChannelMap.values());
        if (!duplicateData.isEmpty()) {
            logger.info("[目录查询]收到的数据存在重复： {}", duplicateData);
        }
        if (CollectionUtils.isEmpty(channels)) {
            logger.info("通道重设，数据为空={}", deviceChannelList);
        }
    }

    public List<DeviceChannel> getChannels() {
        return channels;
    }

    public List<DeviceChannel> getAddChannels() {
        return addChannels;
    }

    public List<DeviceChannel> getUpdateChannels() {
        return updateChannels;
    }

    public List<DeviceChannel> getDeleteChannels() {
        return deleteChannels;
    }
}
